package com.wsy.dao;

import com.wsy.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class CustomerDao {

    @Autowired
    JdbcTemplate template;

    public Customer getUserByAccount(String account) {
        try {
            return template.queryForObject("select * from customer where account = ?",
                    new BeanPropertyRowMapper<>(Customer.class),
                    account);
        } catch (Exception e) {
            return null;
        }
    }

    public boolean saveUser(Customer user) {
        System.out.println(user);
        return template.update("insert into customer (customerID, account, `name` , password) values (null,?, ?, ?)",
                 user.getAccount(),user.getName(), user.getPassword()) > 0;
    }

    public boolean setTokenToUser(String token, Integer id) {
        return template.update("update customer set token = ? where customerID = ?", token, id) > 0;
    }

    public Customer getUserByToken(String token) {
        try {
            return template.queryForObject("select * from customer where token = ?",
                    new BeanPropertyRowMapper<>(Customer.class),
                    token);
        } catch (Exception e) {
            return null;

        }
    }
}
